package ru.urfc.portal.ui.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.dom.client.Element;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.i18n.client.DateTimeFormat;

public class Utils {

	public static final String TIME_FORMAT = "HH:mm:ss";
	public static final String DATE_FORMAT = "dd.MM.yyyy";
	public static final String SERIALIZE_DATE_FORMAT = "dd.MM.yyyy'T'HH:mm:ss.SSSZ";
	private static DateTimeFormat dateToStrFormat;

	public static String getSimpleName(Class<?> klass) {
		return klass.getName().replaceAll(".*[\\.\\$]", "");
	}

	public static String dateToStr(Date date) {
		if (dateToStrFormat == null) {
			dateToStrFormat = DateTimeFormat.getFormat(DATE_FORMAT);
		}
		return dateToStrFormat.format(date);
	}

	public static String capitalize(String text) {
		return text.substring(0, 1).toUpperCase() + text.substring(1);
	}

	public static String decapitalize(String text) {
		return text.substring(0, 1).toLowerCase() + text.substring(1);
	}

	public static RequestBuilder createGetRequestBuilder(String path) {
		RequestBuilder result = createRequestBuilder("GET", path);
		result.setHeader("Accept", "application/xml");
		return result;
	}

	public static RequestBuilder createPostRequestBuilder(String path) {
		RequestBuilder result = createRequestBuilder("POST", path);
		result.setHeader("Content-Type", "application/json");
		return result;
	}

	public static RequestBuilder createPutRequestBuilder(String path) {
		RequestBuilder result = createRequestBuilder("PUT", path);
		result.setHeader("Content-Type", "application/json");
		return result;
	}

	public static RequestBuilder createDeleteRequestBuilder(String path) {
		RequestBuilder result = createRequestBuilder("DELETE", path);
		result.setHeader("Content-Type", "application/json");
		return result;
	}
	
	private static RequestBuilder createRequestBuilder(String requestType, String restUrl) {
		RequestBuilder result = new RequestBuilder(requestType, restUrl) {
			@Override
			public String getHTTPMethod() {  
				if (super.getHTTPMethod().toLowerCase().matches("(post)|(put)")) {
					return "POST"; //  Yes it's hack for GridDataProxy
				}
				return super.getHTTPMethod();
			}			
		};
		result.setTimeoutMillis(0);
		return result;
	}

	public static String createRestUrl(String... path) {
		String restUrl = "/" + Constants.APP + "/" + Constants.REST;
		for (String item: path) {
			restUrl += "/" + item;
		}
		return restUrl;
	}
	
	public static String extractPageParam(String paramName, String pageParams) {
		if (pageParams.matches(".*" + paramName + "=[\\d\\w]+.*")) {
			return pageParams.replaceAll(".*" + paramName + "=", "").replaceAll("&.*", "");
		}
		return null;
	}

	public static String patchHref(String url, String debugParamValue) {
		String debugParam = Constants.GWT_DEBUG_ARG + "=" + debugParamValue;
		if (url.contains(debugParam)) {
			return url;
		}
		String separator = url.contains("?")? "&": "?";
		final String[] split = url.split("#", 2);
		return  split[0] + separator + debugParam + (split.length == 1 ? "" : "#" + split[1]); 
	}

	public static String toString(List<String> list, String separator) {
		StringBuilder result = new StringBuilder();
		for (String item: list) {
			if (result.length() > 0) {
				result.append(separator);
			}
			result.append(item);
		}
		return result.toString();
	}

	public static void show(Element element) {
		element.removeAttribute("style");		
	}

	public static void hide(Element element) {
		element.setAttribute("style", "display:none");
	}

	static public interface DateTransformer {
		Date parse(String value) throws Exception;
		String format(Date date);
	}

	static public class DateTransformerImpl extends DateTimeFormat implements DateTransformer {
	
		protected DateTransformerImpl(String pattern) {
			super(pattern);
		}

	}
	
	static public class DateTransformerFactory {
		
		public DateTransformer create(String pattern) {
			return new DateTransformerImpl(pattern);			
		}
		
	}
		
	static public DateTransformerFactory dateTransformerFactory = new DateTransformerFactory();
	
	public static Date parseDate(String pattern, String value) {
		try {
			DateTransformer transformer = dateTransformerFactory.create(pattern);
			return transformer.parse(value);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	public static String formatDate(String pattern, Date date) {
		DateTransformer transformer = dateTransformerFactory.create(pattern);
		return transformer.format(date);
	}
	
}